package com.example.service;

import com.example.config.datasource.DynamicDataSourceSwitcher;
import com.example.config.datasource.MyDataSource;
import com.example.entity.Account;
import com.example.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author ZHAO Yudong
 * @Date 2021/12/7 15:45
 * @description：
 */
@Service
public class BankService {
    @Autowired
    private AccountMapper accountMapper;


    @MyDataSource(DynamicDataSourceSwitcher.Slave1)
    public List<Account> selectAll() {
        List<Account> accounts = accountMapper.findAll();
        return accounts;
    }

    @Transactional
    @MyDataSource(DynamicDataSourceSwitcher.Slave1)
    public int add(Integer money){

        Example example = new Example(Account.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cardNo","6029621011000");
        Account lidalei = accountMapper.selectOneByExample(example);

        Example example2 = new Example(Account.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("cardNo","6029621011001");
        Account hanmeimei = accountMapper.selectOneByExample(example2);


        lidalei.setMoney(lidalei.getMoney()-money);
        hanmeimei.setMoney(hanmeimei.getMoney()+money);
        int x = accountMapper.updateByPrimaryKeySelective(lidalei);
        int y = accountMapper.updateByPrimaryKeySelective(hanmeimei);
        int i=1/0;
        return x&y;
    }

}
